package com.umeng.kaoti2.fragment;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.mvplibrary.ui.fragment.BaseMvpFragment;
import com.umeng.kaoti2.ApiService;
import com.umeng.kaoti2.R;
import com.umeng.kaoti2.adapter.FirstAdapter;
import com.umeng.kaoti2.data.BannerData;
import com.umeng.kaoti2.data.HighOpinionBean;
import com.umeng.kaoti2.model.MainModel;
import com.umeng.kaoti2.presenter.MainPresenter;
import com.umeng.kaoti2.view.MainView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class FirstFragment extends BaseMvpFragment<MainView, MainModel, MainPresenter> implements MainView {

    @BindView(R.id.banner_id)
    Banner bannerId;
    @BindView(R.id.tv_koubei)
    TextView tvKoubei;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        initBanners();
        presenter.getData();
    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_first;

    }

    @Override
    public void onSuccess(List<HighOpinionBean.ListBean> listBeans) {
        Log.e("TAG", "onSuccess: " + listBeans);

        rvList.setLayoutManager(new LinearLayoutManager(context));
        FirstAdapter firstAdapter = new FirstAdapter(listBeans, context, R.layout.layout_item);
        rvList.setAdapter(firstAdapter);
        rvList.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
    }

    private void initBanners() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("cityid",1);
        map.put("operaVersion","5.1.1");
        new Retrofit.Builder()
                .baseUrl(ApiService.HighUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getBannerData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<BannerData>() {
                    @Override
                    public void onNext(BannerData bean) {
                        ArrayList<String> ImageUrls = new ArrayList<>();
                        if (bean.getData().getList().size()>0&&bean.getData().getList()!=null) {
                            for (int i = 0; i < bean.getData().getList().size(); i++) {
                                ImageUrls.add(bean.getData().getList().get(i).getThumb());
                            }
                        }
                        Log.d("TAG", "onNext: "+ImageUrls.toString());
                        bannerId.setImages(ImageUrls)
                                .setImageLoader(new ImageLoader() {
                                    @Override
                                    public void displayImage(Context context, Object path, ImageView imageView) {
                                        Glide.with(context).load(path).into(imageView);
                                    }
                                }).start();
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onFailed(String error) {
        Log.d("TAG", "onFailed: "+error);
    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}